import {Component, EventEmitter, Input, OnInit, Output, SimpleChanges} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {BaseService} from '../../../shared/service/base.service';
import {ApiUrl} from '../../../config/api-url';

@Component({
  selector: 'app-device-type-edit',
  templateUrl: './device-type-edit.component.html',
  styleUrls: ['./device-type-edit.component.less']
})
export class DeviceTypeEditComponent implements OnInit {

  @Input()
  isVisible = false;
  @Input()
  inputDevice: any;

  @Output()
  emitVisible: EventEmitter<any> = new EventEmitter();
  isOkLoading: any;
  isSpinning: any;

  vendingMachineType: 30018;
  lockTime: number;
  hierarchySequence: 0;
  freightLaneSequence: 0;
  reverseSequence: 0;
  feedbackSequence: 0;
  deliveryTime: number;
  payLimitTime: number;
  // feedback: 0;


  constructor(private fb: FormBuilder, private baseService: BaseService) {
  }

  ngOnInit(): void {
  }

  ngOnChanges(): void {
    if (this.inputDevice != null && this.inputDevice){
      this.vendingMachineType = this.inputDevice.loginId;
      this.hierarchySequence = this.inputDevice.hierarchySequence;
      this.freightLaneSequence = this.inputDevice.freightLaneSequence;
      this.reverseSequence = this.inputDevice.reverseSequence;
      this.feedbackSequence = this.inputDevice.feedbackSequence;
      this.payLimitTime = this.inputDevice.payLimitTime;

      this.lockTime = this.inputDevice.lockTime;
      this.deliveryTime = this.inputDevice.deliveryTime;
      console.log(this.lockTime);
      // if (this.inputDevice.feedback != null) {
      //   this.feedback = this.inputDevice.feedback;
      // }else{
      //   this.feedback = 0;
      // }
    }
  }

  handleCancel() {
    this.isOkLoading = false;
    this.isSpinning = false;
    this.isVisible = false;
    this.emitVisible.emit(this.isVisible);
  }

  handleOk() {
    this.isOkLoading = true;
    this.isSpinning = true;

    let params = {
      deviceNo : this.inputDevice.deviceNo,
      vendingMachineType: this.vendingMachineType,
      hierarchySequence: this.hierarchySequence,
      freightLaneSequence: this.freightLaneSequence,
      reverseSequence: this.reverseSequence,
      feedbackSequence: this.feedbackSequence,
      lockTime: this.lockTime,
      deliveryTime: this.deliveryTime,
      payLimitTime: this.payLimitTime,
      // feedback: this.feedback,
    };

    this.baseService.post(ApiUrl.DeviceUrl + '/vendingParameterSettings', params).subscribe((res) => {
      if (res.status == 0) {
        this.handleCancel();
        this.baseService.layer.messageSuccess('提交成功');
      } else {
        this.isOkLoading = false;
        this.isSpinning = false;
        this.baseService.layer.messageError('修改失败：' + res.msg);
      }
    });
  }
}

